package cn.zyn.eduservice.controller;

import cn.zyn.commonutils.R;
import cn.zyn.eduservice.entity.EduTeacher;
import cn.zyn.eduservice.entity.vo.TeacherQuery;
import cn.zyn.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ：zyn
 * @date ：Created in 2022/4/18 10:35
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    //http://localhost:8001/swagger-ui.html
    //访问地址；  http://localhost:8001/eduservice/teacher/findAll
    //注入service
    @Autowired
    private EduTeacherService eduTeacherService;

    //1、查询所有讲师数据
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")//加不加斜杆都可以
    public R findAllTeacher(){
        //调用service方法
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //2.逻辑删除
    //使用postman测试
    //rest浏览器无法测试delete提交
    //浏览器只能测试get
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        return flag?R.ok():R.error();
    }



    //3.分页查询教师信息
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageList(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        //创建Page对象
        //两个参数page代表当前页   limit代表每页记录数
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, null);

        //每页数据
        List<EduTeacher> records = pageTeacher.getRecords();
        //获取总记录数
        long total = pageTeacher.getTotal();
        //两个data
        //第二种返回方式
//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);
        //另外一种
        return R.ok().data("total", total).data("rows", records);
    }


    //4.条件分页查询教师信息
    @ApiOperation(value = "条件分页讲师列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询条件对象", required = false)
                    @RequestBody(required = false) TeacherQuery teacherQuery){

        //创建Page对象
        //两个参数current代表当前页   limit代表每页记录数
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询  mybatis 动态sql if
        //判断条件值是否为空
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //name是表中的字段名
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //ge大于等于
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //le小于等于
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, wrapper);
        //每页数据
        List<EduTeacher> records = pageTeacher.getRecords();
        //获取总记录数
        long total = pageTeacher.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    //5.添加讲师
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R addTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        //添加
        boolean save=eduTeacherService.save(teacher);

        System.out.println(teacher);
        return save?R.ok():R.error();
    }


    //6、根据Id查询讲师
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }


    //7。更新讲师
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){
            boolean flag = eduTeacherService.updateById(eduTeacher);
            return flag?R.ok():R.error();
    }


}

